#include "main.h"

u8               WALL_LEVEL = 2;
u8				 start_buzzcnt;

DeviceInfo_t     devi_info;

SYSTEM_t         system;
Bit8_ut          system_errs;	

UART_t           panel_comm;
UART_t           motor_comm;
UART_t		     screen_comm;


Smoker_t         smoker;		
MOTOR_CMD_t      motor_cmd;
MOTOR_STATE_t    motor_state;

Gas_t            gasCtrl;

Moto_t  		 board;				
MotoPara_t       boardPara;

SteamSta_t       steamCtrl;
SteamPara_t      steamPara;

SteamSta_t       bakeCtrl;
BakePara_t		 bakePara;


Fireoff_t        fireoff_L;
Fireoff_t        fireoff_R;

Heat_t			 ht_Lbom_out; // 烤箱顶部加热管//左加热盘
Heat_t			 ht_Otop_out; //蒸箱顶部加热管500W//上外 
Heat_t			 ht_Itop_out; //蒸盘外1000W//上内 
Heat_t			 ht_Obom_out; //无//蒸盘外
Heat_t			 ht_Ibom_out; //蒸盘内1000W//蒸盘内 
Heat_t			 ht_pre_out;


bool            light_f = 0;     //照明灯输出
bool            atmos_f = 0;     //氛围灯输出
bool            boxLight_f = 0;  //箱体灯输出
bool            boxlock_f = 0;   //门锁
bool            smokerpower_f = 0;
bool            klight_f;
bool            klock;
bool			zpre12V_f;
bool			kpre12V_f;

bool            cycle_f = 0;		//循环
bool            cold_f = 0;     	//冷却

u8				buzz_mode;       //蜂鸣器


bool            cut_stove_f   = 0; //点火器电源
bool            top_keepwarm_f;
bool			wash_f;
u8              gu8_SystemBeginTime;
Long_ut 		eeprom_rec;
bool            gb_start;
u16             worktime;
bool			gb_realtemper;
u8 				gu8_version;
bool			gb_ganscheckon;
bool            gb_firesmallon;
bool			gb_washboxwork;
crom u8         wall_tempr[5] = {0xff,60,90,120,0xff};

/**************************************************/
void process_packet(UART_t * c_uart)
{
	UART_InitStruType y;
    static u8 temp = 0;
    static u8 prework = 0;
	static u8 prework1 = 0;
	static u8 prebake = 0;
	static u8 prwashbox = 0;
    u8 i;
    if (c_uart->uart_id == ID_UART_PANEL)
    {
        gu8_SystemBeginTime = 0;
		if(c_uart->rx_buf[3] == 0x22)
		{
			
			cut_stove_f = 			(c_uart->rx_buf[5])& 0x01;
			top_keepwarm_f =  		(c_uart->rx_buf[5]>>1)& 0x01;
			if((c_uart->rx_buf[5] >> 2) & 0x01)
				fireoff_L.cmd_flag = 1;
			if((c_uart->rx_buf[5] >> 3) & 0x01)
				fireoff_R.cmd_flag = 1;
			if((c_uart->rx_buf[5] >> 4) & 0x01)
				gb_tmconnet = 1;
			board.cmd =				(c_uart->rx_buf[5] >> 5) & 0x01;
            atmos_f = 				(c_uart->rx_buf[5] >> 6) & 0x01;
			light_f = 				(c_uart->rx_buf[5] >> 7) & 0x01;

			boxLight_f = 	(c_uart->rx_buf[6])& 0x01;
			klight_f = 		(c_uart->rx_buf[6]>>1)& 0x01;
			boli.soupwork_f = (c_uart->rx_buf[6]>>3)& 0x01;
			gb_ganscheckon = (c_uart->rx_buf[6]>>5)&0x01;
			if(c_uart->rx_buf[6]&0x10)
			{
				gb_tmrest = 1;
				gb_tmconnet = 0;
			}
			i = (c_uart->rx_buf[6]>>6)& 0x03;
			if(gu8_version != i )
			{
				if(i == 0)//不带天猫精灵
				{
					NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_0, Disable);
					y.UART_StopBits = UART_StopBits_1;   //1位停止位
					y.UART_TxMode = UART_DataMode_8;     //8位数据位，无奇偶校验位
					y.UART_TxPolar = UART_Polar_Normal;  //发送正级性
					
					y.UART_RxMode = UART_DataMode_8;     //8位数据位，无奇偶校验位
					y.UART_RxPolar = UART_Polar_Normal;  //接收正级性
					y.UART_BaudRate = 9600;//19200;            
					y.UART_ClockSet = UART_Clock_1;      //时钟
					UART_Init(UART0, &y);//kmos通信	
					NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_0, Enable);
				}
				else if(i == 1)
				{
					NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_0, Disable);
					y.UART_StopBits = UART_StopBits_1;   //1位停止位
					y.UART_TxMode = UART_DataMode_8;     //8位数据位，无奇偶校验位
					y.UART_TxPolar = UART_Polar_Normal;  //发送正级性
					
					y.UART_RxMode = UART_DataMode_8;     //8位数据位，无奇偶校验位
					y.UART_RxPolar = UART_Polar_Normal;  //接收正级性
					y.UART_BaudRate = 115200;//天猫通信            
					y.UART_ClockSet = UART_Clock_1;      //时钟
					UART_Init(UART0, &y);//kmos通信	
					NVIC_Init(NVIC_UART0_IRQn, NVIC_Priority_0, Enable);
						
				}
			}
			
			gu8_version =	(c_uart->rx_buf[6]>>6)& 0x03;
			

			smoker.level = c_uart->rx_buf[7]  & 0x1f;
			if(c_uart->rx_buf[7]&0x40)	
				gb_firesmallon = 1;
			else
				gb_firesmallon = 0;				
			buzz_mode = c_uart->rx_buf[8] & 0x0f;
			temp = (c_uart->rx_buf[9]>>6) & 0x03;//蒸烤运行状态
			if(temp == 0x01)
			{	
							
				//加热管检测
				if((gb_btubecheck == 0)&&(bakeCtrl.state != STA_WORK)&&(bakeCtrl.state != STA_PAUSE))
				{
					gb_btubecheck = 1;//1;// gu8_PowToStDlayCnt = 180;
					if(gb_stubecheck)
					{
						gb_btchekdly = 1;
					}
					else
						gb_btchekdly = 0;

				}									   
				/*****蒸烤模式**********/   
				temp = (c_uart->rx_buf[9] & 0x3f) +1;                                
				if((prebake != temp))
				{
					bakePara.menu = (BakeModeTYPE)(temp); 
					bakePara.tempr_set = c_uart->rx_buf[10];                       
					Bake_PID_ParaneterSelect(&BAKE_PID);                       
				}						
				prebake = temp;					                     
				bakeCtrl.state = STA_WORK;				
			}
			else if(temp == 0x02)//预约中
			{
				bakeCtrl.state = STA_APPOINT;
				gb_btubecheck = 0;
				prebake = 0;
			}
			else if(temp == 0x03)//暂停
			{
				bakeCtrl.state = STA_PAUSE;
			}
			else 
			{
					
				bakeCtrl.state = STA_NULL;
				bakePara.menu = BWORK_ALLOFF;	
				gb_btubecheck = 0;		
				prebake = 0;
			}	
			/*******在工作状态下温度变化********/
			if((bakeCtrl.state == STA_PAUSE)||(bakeCtrl.state == STA_WORK))
			{
				if(bakePara.tempr_set != c_uart->rx_buf[10])
				{
					bakePara.tempr_set = c_uart->rx_buf[10];                                 
					Bake_PID_ParaneterSelect(&BAKE_PID);                            
				}
			}	
			bakePara.tempr_set = c_uart->rx_buf[10];
			/**********************蒸箱工作*************************/	
			if(c_uart->rx_buf[4] >= 19)
			{
				if((prwashbox == 0)&&(c_uart->rx_buf[16]&0x80))
				{
					gb_washboxwork = 1;
				}
				prwashbox = (c_uart->rx_buf[16]&0x80)?1:0;
			}								
			temp = (c_uart->rx_buf[11]>>6) & 0x03;//蒸箱运行状态
			if(temp == 0x01)
			{
				/*****蒸烤模式**********/   
				temp = (c_uart->rx_buf[11] & 0x3f) +1; 				
				if((gb_stubecheck == 0)&&(steamCtrl.state != STA_WORK)&&(steamCtrl.state != STA_PAUSE) )
				{
					if(temp != WORK_SOFT)
					{
						gb_stubecheck = 1;//1;// gu8_PowToStDlayCnt = 180;
						if(gb_btubecheck)
						{
							gb_stcheckdly = 1;
						}
						else
							gb_stcheckdly = 0;
					}
					else
					{
						gb_stcheckdly = 0;
						gb_stubecheck = 0;
					}

				}  
                               
				if((prework != temp))
				{
					steamPara.menu = (WorkModeTYPE)(temp); 
					steamPara.tempr_set = c_uart->rx_buf[12];                       
					STEAM_PID_ParaneterSelect();
					{ 
						gb_skeeptemper = 0;                                                       
						gb_DryFireFlag = 0;
						gb_WaterProcess = 1; 
						
						if(gu8_draintime <= 15)//排水超过15秒就可以进水||(tempr_bom.cur > 140)
						{
							gu8_AddWaterTime1 = 40;
							if(tempr_bom.cur > 100)
							{
								gu8_AddWatterdelay = 60;
							}							
						}
						else
							gu8_AddWaterTime1 = 0;						
						gu8_draintime = 0;
						if(gb_washboxwork)
						{
							gu8_AddWaterTime1 = 50;
						}
						if(steamPara.menu == WORK_DESCAL)
						{
							gu8_AddWaterTime1 = 80;
						}						
						else if(steamPara.menu == WORK_CHUJUN)
						{
							steamPara.time_min = 0;
							steamPara.time_sec = 0;
							gu8_DisinfWorkStatus = 1;
											
						}	
						else if(steamPara.menu == WORK_SOFT)
						{
							gb_washboxwork = 0;
							gu8_AddWaterTime1 = 0;
						}											
					}                       
				}		
				prework = temp;					                     
				steamCtrl.state = STA_WORK;
				if(prework1 != steamCtrl.state)
				{
					if(tempr_box.cur <= (STEAM_PID.u16_itemtemp-2))
						gb_skeeptemper = 0;
				}
			}
			else if(temp == 0x02)//预约中
			{
				steamCtrl.state = STA_APPOINT;
				gb_stubecheck = 0;
				prework = 0;
			}
			else if(temp == 0x03)
			{
				steamCtrl.state = STA_PAUSE;
			}
			else 
			{
				if((steamCtrl.state == STA_PAUSE)||(steamCtrl.state == STA_WORK))
					gu8_draintime = 30;
				steamCtrl.state = STA_NULL;
				steamPara.menu = WORK_ALLOFF;
				gb_WaterProcess = 0;
				gb_DryFireFlag= 0;	
				gb_stubecheck = 0;			
				prework = 0;
			}	
			prework1 = steamCtrl.state;		
			/*******在工作状态下温度变化********/
			if((steamCtrl.state == STA_PAUSE)||(steamCtrl.state == STA_WORK))
			{
				if(steamPara.tempr_set != c_uart->rx_buf[12])
				{
					if(tempr_box.cur <= (steamPara.tempr_set-2))
						gb_skeeptemper = 0;
					steamPara.tempr_set = c_uart->rx_buf[12];                                 
					STEAM_PID_ParaneterSelect();                           
				}
			}
			steamPara.tempr_set = c_uart->rx_buf[12];
			steamPara.soft_step = c_uart->rx_buf[14];	
			smoker.type = c_uart->rx_buf[16]&0x0F;

		}
		send_panel_comm(c_uart->rx_buf[3]);		
	}

	if (c_uart->uart_id == ID_UART_MOTOR)
	{
		motor_state.state = 0;
		motor_state.err_code = 0;
		motor_state.cur_speed = MAKEWORD(motor_comm.rx_buf[2]&0x0f,motor_comm.rx_buf[1]);
	}
}

void send_panel_comm(u8 cmd)
{
	u16 crc16_temp;
	Bit8_ut  temp;
	/*****************************************/
	//u8 i;
	//static u8 temp_ct = 0;
	
//	if (++temp_ct >= 10 && (panel_comm.sending_f == 0)) //
//	{
//		temp_ct = 0;
//		gb_scope = 1;
//        DataScope_Get_Channel_Data(tempr_box.cur , 1);
//		DataScope_Get_Channel_Data(tempr_bom.cur , 2);
//        DataScope_Get_Channel_Data((fire_L_thcp_ad>>4) , 3);
//		DataScope_Get_Channel_Data((fire_R_thcp_ad>>4), 4);//look_temp	
//		
//		
//		Send_Count = DataScope_Data_Generate(8); //生成10个通道的 格式化帧数据，返回帧数据长度
//		for(i = 0;i<=Send_Count;i++)
//		{
//			panel_comm.tx_buf[i] = DataScope_OutPut_Buffer[i];
//		}
//	
//		
//		panel_comm.sending_f = true;
//			SelectSFRBank1();
//			SBUF1 = panel_comm.tx_buf[0];
//			SelectSFRBank0();
//		return;
//	}
	/*****************************************/
	panel_comm.tx_buf[0] = LEAD_BYTE;
	panel_comm.tx_buf[1] = 0xA5;	//源地址
	panel_comm.tx_buf[2] = 0x5A;	//目标地址
	panel_comm.tx_buf[3] = 0x22;	//命令码
	panel_comm.tx_buf[4] = 25;		//长度
	/****************数据*********************/
	temp.byte = 0;
	temp.bits.bit_0 = board.cmd;
	temp.bits.bit_1 = 0;//(gasCtrl.sta == GAS_ALARM) ? 1 : 0;
	temp.bits.bit_2 = wall_L.on_f;
	temp.bits.bit_3 = wall_R.on_f;
	temp.bits.bit_4 = sta_Lfire;
	temp.bits.bit_5 = sta_Rfire;
	temp.bits.bit_6 = knobl.sta;
	temp.bits.bit_7 = knobr.sta;
	panel_comm.tx_buf[5] = temp.byte;
	if(motor_state.cur_speed >= 2500)
		motor_state.cur_speed = 0;
	panel_comm.tx_buf[6] = motor_state.cur_speed & 0xff;
	panel_comm.tx_buf[7] = (motor_state.cur_speed >> 8);
	panel_comm.tx_buf[8] = rekmosdate[0];	//KOMS风机档位
	if(toptempr_err)
	{
		if(tempr_top.cur == 0xffff)
			panel_comm.tx_buf[9] = 255;
		else if(tempr_top.cur == 0xfff0)
			panel_comm.tx_buf[9] = 254;
	}
	else
		panel_comm.tx_buf[9] = (tempr_top.cur>252)?252:tempr_top.cur;//烤箱温度

	if(boxtempr_err)
	{
		if(tempr_box.cur == 0xffff)

			panel_comm.tx_buf[10] = 255;
		else if(tempr_box.cur == 0xfff0)
			panel_comm.tx_buf[10] = 254;
	}
	else
		panel_comm.tx_buf[10] = (tempr_box.cur>252)?252:tempr_box.cur;//蒸箱温度	
	if(bomtempr_err)
	{
		if(tempr_bom.cur == 0xffff)
			panel_comm.tx_buf[11] = 255;
		else if(tempr_bom.cur == 0xfff0)
			panel_comm.tx_buf[11] = 254;
	}
	else
		panel_comm.tx_buf[11] = (tempr_bom.cur > 252)?252:tempr_bom.cur;//底盘温度

	temp.byte = 0;
	if(steamCtrl.state == STA_WORK) 
		temp.bits.bit_0 = (gu8_AddWaterTime2||gu8_AddWaterTime1)?1:0;
	if(gans_L.error)
		temp.bits.bit_1 = 1;
    if(tempr_wall_L == 0xff)
        temp.bits.bit_2 = 1;
    if(tempr_wall_R == 0xff)
        temp.bits.bit_3 = 1;
	temp.bits.bit_4 = (gb_DryFireFlag)?1:0;
	temp.bits.bit_5 = waterbox.sta;
	temp.bits.bit_6 = door.sta;
	temp.bits.bit_7 = doorbake.sta;
	panel_comm.tx_buf[12] = temp.byte;

	if(motor_rx_err)
		panel_comm.tx_buf[13] = 10;
	else
	{
		panel_comm.tx_buf[13] = 0;
		// if(smoker.err_code)
		// {
		// 	if(smoker.err_code == 1)
		// 		panel_comm.tx_buf[13] = 2;
		// 	else if(smoker.err_code == 2)
		// 		panel_comm.tx_buf[13] = 1;
		// 	else
		// 		panel_comm.tx_buf[13] = smoker.err_code;
		// }
		// else
		// 	panel_comm.tx_buf[13] = 0;
	}
	if((gs_tofaceSTubeError)&&((steamCtrl.state == STA_WORK)||(steamCtrl.state ==STA_PAUSE)))//蒸烤箱故障码
	{
		if(gs_tofaceSTubeError&0x04)
			panel_comm.tx_buf[14] = 4;
		else if(gs_tofaceSTubeError&0x01)
			panel_comm.tx_buf[14] = 2;
		else if(gs_tofaceSTubeError&0x02)
			panel_comm.tx_buf[14] = 3;
			
	}
	else
		panel_comm.tx_buf[14] = 0;
	if((gs_tofaceBTubeError)&&((bakeCtrl.state == STA_WORK)||(bakeCtrl.state == STA_PAUSE)))
	{
		panel_comm.tx_buf[15] = 2;
	}
	else
		panel_comm.tx_buf[15] = 0;//烤箱故障
	if(gu8_version == 0)
		panel_comm.tx_buf[16] = 2;//01天猫数据/2kmos数据
	else
		panel_comm.tx_buf[16] = 1;//01天猫数据/2kmos数据
	if(gu8_version == 0)
	{
		panel_comm.tx_buf[17] = 0;
		if((rekmosdate[1] == 0x68)||(rekmosdate[1] == 0x66))
			panel_comm.tx_buf[17] |= 0x80;//右
		panel_comm.tx_buf[18] = 0;//右
		panel_comm.tx_buf[19] = 0;
		if((rekmosdate[1] == 0x86)||(rekmosdate[1] == 0x66))
			panel_comm.tx_buf[19] |= 0x80;//左
		panel_comm.tx_buf[20] = 0;//左
	}
	else
	{
		panel_comm.tx_buf[17] = ToFace[0];//右
		panel_comm.tx_buf[18] = ToFace[1];//右
		panel_comm.tx_buf[19] = ToFace[2];//左
		panel_comm.tx_buf[20] = 0;//左	
		ToFace[0] = 0;
		ToFace[1] = 0;	
		ToFace[2] = 0;	
	}
    panel_comm.tx_buf[21] = HIBYTE(gans_L.diptempr);
	panel_comm.tx_buf[22] = LOBYTE(gans_L.diptempr);	
	crc16_temp = CRC16_MODBUS(panel_comm.tx_buf, (panel_comm.tx_buf[4] - 2));//(长度-2)
	panel_comm.tx_buf[panel_comm.tx_buf[4] - 2] = LOBYTE(crc16_temp);//
	panel_comm.tx_buf[panel_comm.tx_buf[4] - 1] = HIBYTE(crc16_temp);//

	panel_comm.sending_f = true;
	panel_comm.tx_ct = 0;
	panel_comm.send_dly = 5;
	UART_SendByte(UART1, panel_comm.tx_buf[0]);

}

void send_motor_comm(void)
{
	static u8 send_t = 0;
	static u8 prelevel = 0;
	static u8 dely_stop = 2;
	if(system_sec_f)
	{
		u8_dn(&dely_stop);
	}
	if(prelevel != motor_cmd.mode0)
	{
		dely_stop = 30;
	}
	prelevel = motor_cmd.mode0;	
	if (++send_t >= 10) //100ms
	{
		send_t = 0;
		if((motor_cmd.mode0 == 0)&&(dely_stop == 0)&&(gb_TestFlag == 0))
			return;		
		

		motor_comm.tx_buf[0] = LEAD_BYTE;
		motor_comm.tx_buf[1] = motor_cmd.mode0;
					
		motor_comm.tx_buf[2] = 0; //
		if(smoker.type & 0x01)
			motor_comm.tx_buf[2] = 1;	
		if(smoker.type & 0x02)
			motor_comm.tx_buf[2] |= 0x80;			
		motor_comm.tx_buf[3] = get_check_sum(motor_comm.tx_buf,4);

		motor_comm.sending_f = true;
		motor_comm.tx_ct = 0;
		EUART_SendByte(motor_comm.tx_buf[0]);

	}
}


void comm_err_ctrl(void)
{
	static u8 comm_buzz_ct = 5;
	//static u8 motor_buzz_ct = 5;

	if (comm_rx_err) //没接收到面板数据
    {
		if (system_sec_f)
		{
			if (comm_buzz_ct)
			{
				buzz_start(BUZ_ERR);
				comm_buzz_ct--;
				if (comm_buzz_ct == 0) //5声后关输出
				{
					clear_all_output();

				}
			}
		}
	}else
	{

		comm_buzz_ct = 5;
	}	
}

void device_err_ctrl(void)
{
	/*************过零检测中断不进***************/
	if(steamCtrl.state == STA_WORK)
	{
		if(system_sec_f)
		{
			steamCtrl.powerErr_sec++;
		}
		if (steamCtrl.powerErr_sec >= 5)
		{ //持续5秒不进过零检测中断
			steamCtrl.powerErr_sec = 5;
			
			devi_info.error = ERR_POWER;
//			steamCtrl.state = STA_NULL;
		}
			
	}else
	{
		steamCtrl.powerErr_sec = 0;
	}
	
	
}
void work_init(void)
{
    gu8_SystemBeginTime = 5;
    gu8_STubeError = 0;
    gu8_AddWaterTime1 = 0;
    gu8_AddWaterTime2 = 0;
    gb_DryFireFlag = 0;
    steamPara.menu = WORK_ALLOFF;
    steamCtrl.state = STA_NULL; //关蒸箱	  
	smoker.level = 0;  
    gb_start = 0;
    worktime = 0; 
    lightcnt = 0;  
	gb_tcomd2 = 0;	
	gu8_komssendwait = 10;
	leak_checkdly = 6;
	gu8_draintime = 5;
}

void system_init(void)
{
	clear_all_output();
	//init_all_inputs();
	work_init();
	fire_valueinit();

	
	uart_init(&panel_comm, ID_UART_PANEL);
	uart_init(&motor_comm, ID_UART_MOTOR);
    Euart_init();
    Kuart_init();

	boardPara_init(&boardPara);
	
	buzz_init();
    buzz_start(BUZ_KEY);
	//start_buzzcnt = 3;
}	

